package com.ws.hzuoj.admin.system.menu.web;

import cn.hutool.core.util.StrUtil;
import com.ws.common.easyui.util.Page;
import com.ws.common.easyui.util.PageUtils;
import com.ws.hzuoj.admin.system.menu.entity.Menu;
import com.ws.hzuoj.admin.system.menu.service.IMenuService;
import com.ws.hzuoj.admin.system.menu.util.MenuUtils;
import com.ws.user.permission.entity.Permission;
import com.ws.user.role.service.IRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/system/menu")
@Controller
public class MenuWeb {


    @Autowired
    IMenuService menuService;
    @Autowired
    IRolePermissionService rolePermissionService;

    @RequestMapping("{type}")
    public String list(@PathVariable String type){
        return  null;
    }

    @RequestMapping("/permissionTree")
    @ResponseBody
    public Object permissionTree(String roleId, Page page){
        List<Menu> menus=menuService.permissionTree();

        Map<String ,Object> map=new HashMap<>();
        List<String> checkedKeys=rolePermissionService.listPermissionIdByRoleId(roleId);

        //checkedKeys= MenuUtils.getChecksMenu(menus,checkedKeys);
        map.put("menus",menus);
        map.put("checkedKeys",checkedKeys);
        return map;
    }

    @RequestMapping("/listData")
    @ResponseBody
    public Object listData(Menu menu, Page page){
        page.setOrder("order_No asc");
        List<Menu> menus=menuService.list(menu,page);
        return PageUtils.getEasyUiList(menus);
    }

    @RequestMapping("/listRoot")
    @ResponseBody
    public Object listRoot(Menu menu,Page page){
        page.setOrder("order_No asc");
        menu.setPid("0");
        List<Menu> menus=menuService.list(menu,page);
        return menus;
    }

    @RequestMapping("/menuList")
    @ResponseBody
    public List<Menu> menuList(){
        return menuService.menuList();
    }

    @RequestMapping("read")
    @ResponseBody
    public Object read(Menu menu){
        if(StrUtil.isNotEmpty(menu.getId())){
            menu= menuService.read(menu.getId());
        }
        return  menu;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(Menu menu){
        Integer count=menuService.save(menu);
        return  count;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(String id){
        return  menuService.delete(id);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(Menu menu){
        Integer count=menuService.insert(menu);
        return  count;
    }

    @RequestMapping("/insertSelective")
    @ResponseBody
    public Object insertSelective(Menu menu){
        return menuService.insertSelective(menu);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(Menu menu){
        return menuService.update(menu);
    }

    @RequestMapping("/listAll")
    @ResponseBody
    public Object listByMap(){
        return  menuService.listAll();
    }
}
